package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.domain.ActivityLog;
import com.foreverwin.jzymes.common.mapper.ActivityLogAdviceMapper;
import com.foreverwin.jzymes.common.mapper.ActivityLogMapper;
import com.foreverwin.jzymes.common.service.IActivityLogAdviceService;
import com.foreverwin.jzymes.common.service.IItemGroupAdviceService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Service
public class ActivityLogAdviceServiceImpl implements IActivityLogAdviceService {
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    ActivityLogAdviceMapper activityLogAdviceMapper;
    @Autowired
    ActivityLogMapper activityLogMapper;

    /**
     * 查询本体指定时间之后在工序过站记录
     *
     * @param site
     * @param carrier
     * @param operation
     * @param fromDate
     * @return
     */
    public List<ActivityLog> findCarrierActivityLogOnOperation(String site, String carrier, String operation, Date fromDate ){
        /**检查站点*/
        siteAdviceService.existSite( site );
        /**检查本体编号*/
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ) );
        /**检查工序*/
        Assert.hasText( operation, I18nUtil.getI18nText( "common.operationNotBlank" ) );
        /**检查日期*/
        Assert.notNull( fromDate, I18nUtil.getI18nText( "common.dateNotNull" ) );

        List<ActivityLog> activityLogList = activityLogAdviceMapper.selectCarrierActivityLogOnOperation( site, carrier, operation, fromDate );
        return activityLogList;
    }

    /**
     * 保存过站记录
     *
     * @param sfc
     * @param carrier
     * @param dateCode
     * @param resource
     * @param passQty
     * @param passDate
     */
    public void saveActivityLog( String site, String sfc, String itemBo, String carrier, String dateCode, String resource, Long passQty, Date passDate ){
        String operation = resourceAdviceService.findOperationByResource( site, resource );
        saveActivityLog( site, sfc, itemBo, carrier, dateCode, resource, operation, passQty, passDate );
    }

    /**
     * 保存过站记录
     *
     * @param sfc
     * @param carrier
     * @param dateCode
     * @param resource
     * @param operation
     * @param passQty
     * @param passDate
     */
    public void saveActivityLog( String site, String sfc, String itemBo, String carrier, String dateCode, String resource, String operation, Long passQty, Date passDate ){
        ActivityLog activityLog = new ActivityLog();
        activityLog.setSite( site );
        activityLog.setCarrier( carrier );
        activityLog.setDateCode( dateCode );
        activityLog.setSfc( sfc );
        activityLog.setMachineType( itemGroupAdviceService.findItemGroupBySite( site ) );
        activityLog.setItemBo( itemBo );
        activityLog.setResrce( resource );
        activityLog.setOperation( operation );
        activityLog.setQty( passQty );
        activityLog.setCreateUserBo( BOHandleUtil.buildUserHandle( site, CommonMethods.getUserId() ) );
        activityLog.setCreatedDateTime( passDate );
        activityLog.setHandle( BOHandleUtil.buildActivityLogHandle( activityLog.getSite(), activityLog.getSfc(), activityLog.getResrce(), activityLog.getCreatedDateTime() ) );
        activityLogMapper.insertActivityLog( activityLog );
    }
}
